package rabbitmq.direct;

import com.rabbitmq.client.*;
import org.pb.basic.util.ConnectionUtils;

import java.io.IOException;

/**
 * 定向模式的消费者
 * @author pb
 */
public class Consumer1 {

    private static final String QUEUE = "direct_queue";

    private static final String EXCHANGE = "direct_exchange";

    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();
        // 声明队列
        channel.queueDeclare(QUEUE, false, false, false, null);
        // 将队列绑定到交换机
        channel.exchangeBind(QUEUE, EXCHANGE, "consumer");

        DefaultConsumer consumer = new DefaultConsumer(channel) {

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body, "utf8");
                System.out.println("消费者1" + msg);
            }
        };

        // 消费者监听消息队列
        channel.basicConsume(QUEUE, true, consumer);
    }


}
